package com.leco.demo.spring;

import com.leco.demo.spring.beans.Bean1;
import com.leco.demo.spring.beans.Bean2;
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;

/**
 * @author greg
 * @version 2022/10/21
 * 研究@Autowire原理
 **/
public class AutowireAnnoDemo {
    public static void main(String[] args) {
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        beanFactory.registerSingleton("bean2", new Bean2());
        AutowiredAnnotationBeanPostProcessor processor = new AutowiredAnnotationBeanPostProcessor();
        processor.setBeanFactory(beanFactory);
        Bean1 bean1 = new Bean1();
        System.out.println(bean1);
        /**
         * @Autowire注入步骤：
         * 1、获取bean的哪些方法和字段上包含@Autowire注解，将这些信息封装到一个InjectionMetadata metadata对象中
         * 2、执行metadata.inject方法：
         *   a)如果@Autowire在字段上，最终是通过反射field.set(bean, value)注入属性的值
         *   b)如果@Autowire在方法上，最终是通过反射method.invoke(bean, arguments)注入属性的值
         */
        processor.postProcessProperties(null, bean1, "bean1");
        System.out.println(bean1);
    }
}
